package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/11  10:13
 *  @描述       ${TODO}
 */


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.StaffAdministrationAdapter;
import com.yanq.liancar.bean.OrderListBean;
import com.yanq.liancar.bean.StaffAdministrationBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.view.StaffSideLetterBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StaffAdministrationActivity extends BaseToolbarActivity {
    @BindView(R.id.SideLetterBar)
    StaffSideLetterBar SideLetterBar;
    @BindView(R.id.staff_recycleview)
    ListView recyclerview;
    @BindView(R.id.tv_letter_overlay)
    TextView overlay;
    @BindView(R.id.tv_add)
    TextView tv_add;

    private StaffAdministrationAdapter mAdapter;
    private List<StaffAdministrationBean.DataBean> mBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staffadministration);
        ButterKnife.bind(this);
        mTvTitle.setText("员工管理");
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white).init();

        initRecyclerview();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getStaffList();
    }

    private void initRecyclerview() {
        mAdapter = new StaffAdministrationAdapter(this, mBeanList);
        recyclerview.setAdapter(mAdapter);
        SideLetterBar.setOverlay(overlay);
        SideLetterBar.setOnLetterChangedListener(new StaffSideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mAdapter.getLetterPosition(letter);
                recyclerview.setSelection(position);
            }
        });
        mAdapter.setOnClickListener(new StaffAdministrationAdapter.OnClickListener() {
            @Override
            public void onCityClick(int id) {
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                RxActivityTool.skipActivity(StaffAdministrationActivity.this, StaffEditActivity.class,bundle);
            }
        });
    }

    @OnClick({R.id.tv_letter_overlay, R.id.tv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                Bundle bundle = new Bundle();
                bundle.putInt("id", 0);
                RxActivityTool.skipActivity(this, StaffEditActivity.class, bundle);
                break;
            case R.id.tv_letter_overlay:
                break;
        }
    }

    //获取员工列表
    private void getStaffList() {
        OkGo.<String>post(Config.s + "llc/selectEmpList")
                .tag(this)
                .params("token", UserBean.getbToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        StaffAdministrationBean orderListBean = gson.fromJson(response.body(), StaffAdministrationBean.class);
                        if (orderListBean.isIsSuccess()) {
                            // if (isClear) {
                            mBeanList.clear();
                            //}
                            mBeanList.addAll(orderListBean.getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }

}
